package com.yujn.order.interceptor;

import cn.hutool.json.JSONUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class FeignAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 业务逻辑  模拟认证逻辑
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();

        if(null != attributes){
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader("token");
            System.out.println("FeignAuthRequestInterceptor token:" + JSONUtil.toJsonStr(attributes));
        }
    }
}
